package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityEnterpriseAuthDetailBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.AuthDetailEntity;
import com.sgb.kjwl.view.event.CategoryEvent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.self.userInfo.RegisterActivity3;
import com.sgb.kjwl.viewmodel.self.EnterpriseAuthDetailModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

/**
 * 企业认证详情
 */
public class EnterpriseAuthDetailActivity extends AppActivity {
    private ActivityEnterpriseAuthDetailBinding binding;
    String userNo;

    @Inject
    EnterpriseAuthDetailModel viewModel;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //设置状态栏为暗
    @Override
    protected boolean statusBarDark() {
        return true;
    }

    public static void start(Context context, String userNo) {
        Intent intent = new Intent(context, EnterpriseAuthDetailActivity.class);
        intent.putExtra("userNo", userNo);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        //双向绑定Activity和View
        binding = DataBindingUtil.setContentView(EnterpriseAuthDetailActivity.this, R.layout.activity_enterprise_auth_detail);

        userNo = getIntent().getStringExtra("userNo");
        viewModel.getUserInfo(userNo);

        binding.nsView.setVisibility(View.INVISIBLE);
        binding.include.titleView.setText("认证详情");
        binding.include.ivBack.setOnClickListener(view -> finish());

    }

    public void showDetail(AuthDetailEntity entity) {
        binding.nsView.setVisibility(View.VISIBLE);
        binding.tvEnterpriseBusinessType.setText(entity.getNewMainBusinessName());
        binding.setEntity(entity);
        if (entity.getCompanyCurrentStatus() == null)
            return;
        switch (entity.getCompanyCurrentStatus()) {
            case "SUCCESS"://认证成功
                binding.tvAuthStatus.setText("认证成功");
                binding.llHasReason.setVisibility(View.GONE);
                binding.llNoReason.setVisibility(View.VISIBLE);
                binding.llReAuthen.setVisibility(View.GONE);
                Glide.with(this).load(R.mipmap.authen_success).into(binding.ivAuthenStatus);
                break;
            case "FAIL"://认证失败
                binding.tvAuthStatus.setText("审核失败");
                binding.llHasReason.setVisibility(View.VISIBLE);
                binding.llNoReason.setVisibility(View.GONE);
                binding.llReAuthen.setVisibility(View.VISIBLE);
                binding.tvReAuthen.setOnClickListener(view -> RegisterActivity3.start(this, true, true, userNo));//重新认证
                Glide.with(this).load(R.mipmap.authen_fail).into(binding.ivAuthenFail);
                break;
            case "STANDBY"://认证中
                binding.tvAuthStatus.setText("审核中");
                binding.llHasReason.setVisibility(View.GONE);
                binding.llNoReason.setVisibility(View.VISIBLE);
                binding.llReAuthen.setVisibility(View.GONE);
                Glide.with(this).load(R.mipmap.authen_ing).into(binding.ivAuthenStatus);
                break;
            case "NONE":
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(CategoryEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_RE_AUTH_COM) {
            finish();
        }
    }
}
